<?php
// 本类由系统自动生成，仅供测试用途
class ParentAction extends CommonAction {
    public function index(){
        $this->display();
    }

    Public function message_list()
    {
        $mstudent =M('student');
        $mapst['parent_id']=$_SESSION['parent_id'];
        $studentlist =$mstudent->where($mapst)->select();
        $this->assign('studentlist',$studentlist);
        $this->display();
    }


    public function proc_list()
    {

    	$mapc['class_id']=$_REQUEST['cid'];
    	$maps['school_id']=getinfo($mapc['class_id'],'class','school_id');
    	$mschool_proclamation=M('school_proclamation');
    	$mclasss_proclamation=M('class_proclamation');
    	$schoolplist = $mschool_proclamation->where($maps)->order('id desc')->select();
    	$classplist = $mclasss_proclamation->where($mapc)->order('id desc')->select();

    	$this->assign('schoolplist',$schoolplist);
		$this->assign('classplist',$classplist);
	
    	$this->display();
    }
    public function proc_content()
    {
    	$t=$_REQUEST['t'];
    	if ($t=='school')
    	{
    		$proclamation=M('school_proclamation');
    	}
    	else if ($t=='class')
    	{
    		$proclamation=M('class_proclamation');
    	}
    	$pinfo = $proclamation->find($_REQUEST['id']);
    	$this->assign('pinfo',$pinfo);
    	$this->display();


    }

    public function remark_content()
    {
    	$maps['student_id']=$_REQUEST['sid'];
    	$mstudent_remark=M('student_remark');
    	$remarklist=$mstudent_remark->where($maps)->select();
    	$this->assign('remarklist',$remarklist);
    	$this->assign('student_id',$maps['student_id']);
    	$this->display();

    }
    public function remark_save()
    {
    	$data=$this->_post();
    	if (empty($data['JXPY']))
    	{
    		$this->error('请填写内容');
    	}
    	$data['FBSJ']=time();
    	$data['parent_id']=$_SESSION['parent_id'];
    	$data['teacher_or_parent']=2;
		$mstudent_remark=M('student_remark');
    	
    	if ($mstudent_remark->add($data))
    	{
    		$this->redirect('/Parent/remark_content/sid/'.$data['student_id']);
    	}
    }

    public function work_list()
    {
    	$mapc['class_id']=$_REQUEST['cid'];
    	$mwork=M('student_work');
    	$worklist =$mwork->where($mapc)->order('id desc')->select();
    	$this->assign('worklist',$worklist);
    	$this->display();
    	
    }
    public function work_content()
    {
    	$mwork=M('student_work');
    	$winfo = $mwork->find($_REQUEST['id']);
    	$this->assign('winfo',$winfo);
    	$this->display();
    }

    public function im_list()
    {
    	$mstudent =M('student');
    	$mapst['parent_id']=$_SESSION['parent_id'];
    	$mteacher_power=M('teacher_power');
    	$studentlist =$mstudent->where($mapst)->select();
    	for($i=0;$i<count($studentlist);$i++)
        {
        	$class_id='';
        	$class_id = $studentlist[$i]['class_id'];

        	$studentlist[$i]['ctrl_teacher'] = getinfo($class_id,'class','teacher_id');
        	if (!empty($class_id))
        	{
        		$studentlist[$i]['powerlist'] = $mteacher_power->where('class_id = '.$class_id.' and teacher_id !='.$studentlist[$i]['ctrl_teacher'])->select();
        	
        	}

       


        }
    	$this->assign('studentlist',$studentlist);




		$this->display();
    }
    public function im_parent_list()
    {
    	$mapst['class_id']=$_REQUEST['cid'];
    	$mstudent=M('student');
    	$studentlist=$mstudent->where($mapst)->select();
    	$this->assign('cid',$_REQUEST['cid']);
    	$this->assign('studentlist',$studentlist);
    	$this->display();
    }

    public function im()
    {
    	$tid =$_REQUEST['tid'];
    	$parent_id =$_SESSION['parent_id'];

    	$this->assign('tid',$tid);
    	$this->assign('parent_id',$parent_id);
    	
    	$user_id =getinfo($_SESSION['parent_id'],'parent','user_id');
    	$tuser_id =getinfo($tid,'teacher','user_id');
    	$message=M('message');
    	$meslist =$message->where('send_id ='.$user_id .' and receive_id='.$tuser_id)->order('time desc')->select();
    	$this->assign('user_id',$user_id);
    	$this->assign('tuser_id',$tuser_id);
    	$this->assign('tid',$tid);
    	
    	$this->assign('meslist',$meslist);
    	$this->display();
    }
    public function imp()
    {
    	$pid =$_REQUEST['pid'];
    	$cid =$_REQUEST['cid'];
    	$this->assign('cid',$cid);
    	$this->assign('pid',$pid);
    	$parent_id =$_SESSION['parent_id'];
		$this->assign('parent_id',$parent_id);


    	$user_id =getinfo($_SESSION['parent_id'],'parent','user_id');
    	$tuser_id =getinfo($pid,'parent','user_id');
    	$message=M('message');
    	$meslist =$message->where('send_id ='.$user_id .' and receive_id='.$tuser_id)->order('time desc')->select();
    	$this->assign('user_id',$user_id);
    	$this->assign('tuser_id',$tuser_id);
    	$this->assign('tid',$tid);
    	
    	$this->assign('meslist',$meslist);
    	$this->display();
    }

    public function im_save()
    {
    	$data=$this->_post();
    	$data["time"] = time();
    	$data["child_id"] = '-1';
    	$data["type"] = 'text';
		$data["state"] = 0;
    	$message = M("message");

    	$systype = getinfo($data['receive_id'],'user','systype');
    	$token = getinfo($data['receive_id'],'user','token');
    	$phone = getinfo($data['receive_id'],'user','SJHM');
    	

    	if ($systype=='android'){
					require_once './SAM/php_sam.php';
					$conn = new SAMConnection();
					$conn->connect(SAM_MQTT, array(SAM_HOST => 'www.appbaba.com', SAM_PORT => 1883));  
			 		$androidcon = '{"type":"'.$data["type"].'","child_id":"'.$child_id.'","send_id":"'.$data['send_id'].'","receive_id":"'.$data['receive_id'].'","time":"'.$data["time"].'","content":"'.$data['content'].'"}';						
					$msgCpu = new SAMMessage($androidcon);
					$conn->send('topic://999/'.$token, $msgCpu);
					$conn->disconnect(); 
					
		}
		elseif($systype=='ios'){
					$body['aps'] = array( 'alert' => $data['content'],  'sound' => 'default' ); 
					$body['action'] = array( 'type' => $data['type'],'child_id' => $child_id,  'send_id' => $data['send_id'],  'receive_id' => $data['receive_id'], 'content' => $data['content'], 'time' =>$data["time"]  ); 
					$ctx = stream_context_create();
					$pem = 'e:\appbaba\statics\pem\10\apns-dis.pem';
					stream_context_set_option($ctx,"ssl","local_cert",$pem);
					$fp = stream_socket_client("ssl://gateway.push.apple.com:2195", $err, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
					if (!$fp) {/*  echo "Failed to connect $err $errstrn"; */
						$arr['im']['state'] = 'error';
					}else{
						$payload = json_encode($body); 
						
						$msg = chr(0) . pack("n",32) . pack("H*", str_replace(' ', '', $userinfo[0]['token'])) . pack("n",strlen($payload)) . $payload;
						$result = fwrite($fp, $msg, strlen($msg));
						if (!$result){
							//echo "sending error : dev:".$userinfo[0]['token'];
							//$arr['im']['state'] = 'error';
							$this->error('推送失败');
						}
					}
		}elseif($systype==''){
				$sms = $this->sendsms($phone,$data['content']);
				if($sms!==1){
						//$arr['im']['state'] = 'error';
						$this->error('推送失败'.$phone);
				}

		}
		//if($arr['im']['state'] == 'success'){
		$lastInsId = $message->add($data);
		//}
		$this->success('发送成功');

				
    	//$data=$this->_post();

    }
	
	//vleugel 
    public function im_get()
    {
		$receive_id=$_POST['receive_id'];
		$send_id=$_POST['send_id'];
		$message=M('message');
		
		$sql="select send_id,content,time,type from message where receive_id='".$receive_id."' and send_id='".$send_id[$i]."' and state=0";
		$res = $message->query($sql); 
		$message->query("update message set state=1 where receive_id='".$receive_id."' and send_id='".$send_id[$i]."'"); 

		$get_mes_info="<get_mes_info>";
		foreach($res as $row){
			$get_mes_info.="<mes><send_id>".$row['send_id']."</send_id><content>".$row['content']."　</content><time>".date('Y-m-d H:i:s',$row['time'])."</time><type>".$row['type']."</type></mes>";
		}
		
		$get_mes_info.="</get_mes_info>";
		header("content-type:text/xml;charset=utf-8");
		echo $get_mes_info;
    }

    public function functions()
    {
    	$user_id = getinfo($_SESSION['parent_id'],'parent','user_id');
    	$this->assign('user_id',$user_id);
    	$this->display();
    }


    public function user_list()
    {
    	$parent_id=$_SESSION['parent_id'];
    	$this->assign('parent_id',$parent_id);
    	$mstudent=M('student');
    	$mapst['parent_id']=$_SESSION['parent_id'];
    	$studentlist =$mstudent->where($mapst)->select();
    	$this->assign('studentlist',$studentlist);
	
    	$this->display();
    }
    public function user_info()
    {
    	$parent_id=$_SESSION['parent_id'];
    	$this->assign('parent_id',$parent_id);
    	$mstudent=M('student');
    	$mparent=M('parent');
    	$parentinfo = $mparent->find($parent_id);
    	$this->assign('parentinfo',$parentinfo);
    	
    	$mapst['parent_id']=$_SESSION['parent_id'];
    	$studentlist =$mstudent->where($mapst)->select();
    	$this->assign('studentlist',$studentlist);
	
    	$this->display();
    }

    public function school_info()
    {
    	$map['id']=$_REQUEST['id'];
    	$mschool=M('school');
    	$schoolinfo =$mschool->where($map)->find();
    	$this->assign('schoolinfo',$schoolinfo);
    	$this->display();
    }

    public function info_save()
    {
    	$data=$this->_post();
    	$field=$data['id'];
    	$value =$data['value'];
    	$data_v[$field]=$value;
    	$map['id']=$_SESSION['parent_id'];
    	$mparent =M('parent');
    	
    	$info =getinfo($map['id'],'parent',$field);
    	if ($info ==$value)
    	{
    		echo $value;
    		exit;	
    	}
    	if ($mparent->where($map)->save($data_v))
    	{

    		echo $value;
    	}
    	else
    	{
    		echo '';
    		//$this->error('修改失败');
    	}


    }



    public function info_stu_save()
    {
    	$data=$this->_post();
    	$field=$data['id'];
    	$value =$data['value'];
    	//print_r($data);
    	$keyarr =explode('_',$field);
    	$map['id']=$keyarr[1];
    	$data_v[$keyarr[0]]=$value;
    	$mstudent =M('student');
    	$info =getinfo($map['id'],'student',$keyarr[0]);
    	if ($info ==$value)
    	{
    		echo $value;
    		exit;	
    	}

    	if ($mstudent->where($map)->save($data_v))
    	{
    		echo $value;
    	}
    	else
    	{
    		echo '';
    		//$this->error('修改失败');
    	}
    	

    }
    public function av_change_s()
    {
        $student_id=$_REQUEST['id'];
        $this->assign('student_id',$student_id);
        $mstudent=M('student');
        $studentinfo = $mstudent->find($student_id);
        $this->assign('studentinfo',$studentinfo);
        $this->display();
    }

    public function av_change()
    {
    	$parent_id=$_SESSION['parent_id'];
    	$this->assign('parent_id',$parent_id);
    	$mparent=M('parent');
    	$parentinfo = $mparent->find($parent_id);
    	$this->assign('parentinfo',$parentinfo);
    	

    	$this->display();	
    }
    public function im_info()
    {
        $tid=$_REQUEST['tid'];
        $this->assign('tid',$tid);
        $mteacher=M('teacher');
        $map['id']=$tid;
        $teacherinfo =$mteacher->find($tid);
        $this->assign('teacherinfo',$teacherinfo);
        $this->display();
    }
    public function imp_info()
    {
        $pid=$_REQUEST['pid'];
        $cid=$_REQUEST['cid'];
        $this->assign('pid',$pid);
        $this->assign('cid',$cid);
        
        $mparent=M('parent');
        $map['id']=$tid;
        $parentinfo =$mparent->find($pid);
        $this->assign('parentinfo',$parentinfo);
        $this->display();
    }
    public function parent_save_pic()
    {
    	$file = $_FILES['pic']['name'];

    	import('ORG.Net.UploadFile');
    	$id=$_SESSION['parent_id'];
        $upload = new UploadFile();// 实例化上传类
        $upload->maxSize  = 3145728 ;// 设置附件上传大小
        $upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->savePath =  './uploads/';// 设置附件上传目录
        if(!$upload->upload()) {// 上传错误提示错误信息
               $this->error($this->error($upload->getErrorMsg()));
                //$this ->_error(308);
        }else{
            $info = $upload->getUploadFileInfo();
            $data['pic'] = 'uploads/'.$info[0]['savename'];
            $m=M('parent');
            $m->where('id='.$id)->save($data);
            $this->redirect('/Parent/av_change');
                
        }
    }
    public function student_save_pic()
    {
        $file = $_FILES['pic']['name'];

        import('ORG.Net.UploadFile');
        $id=$_REQUEST['id'];
        $upload = new UploadFile();// 实例化上传类
        $upload->maxSize  = 3145728 ;// 设置附件上传大小
        $upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->savePath =  './uploads/';// 设置附件上传目录
        if(!$upload->upload()) {// 上传错误提示错误信息
               $this->error($this->error($upload->getErrorMsg()));
                //$this ->_error(308);
        }else{
            $info = $upload->getUploadFileInfo();
            $data['pic'] = 'uploads/'.$info[0]['savename'];
            $m=M('student');
            $m->where('id='.$id)->save($data);
            $this->redirect('/Parent/av_change_s/id/'.$id);
                
        }
    }
    public function chgpass()
    {
        $muser=M('user');
        $userinfo =$muser->find($_SESSION['user_id']);
        if (encrypt($_REQUEST['pass1']) != $userinfo['password_des'])
        {
            $this->error('原密码有错！');
        }
        if ($_REQUEST['pass2']!=$_REQUEST['pass3'])
        {
            $this->error('确认密码与新密码不符！');   
        }
        $data['password_des'] = encrypt($_REQUEST['pass2']);
        $data['password']=$_REQUEST['pass2'];
        $map['id']=$_SESSION['user_id'];
        $muser->where($map)->save($data);
        $this->success('修改成功');
    }

}